import { _decorator, Component, Sprite, UITransform } from 'cc';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM } from '../Enums';
import EventManager from '../Runtime/EventManager';
import ResourceManager from '../Runtime/ResourceManager';
import { IEntity } from '../Levels';
import { TILE_HEIGHT, TILE_WIDTH } from '../Scripts/Tile/TileManager';
import { PlayerStateMachine } from '../Scripts/Player/PlayerStateMachine';
import { StateMachine } from './StateMachine';
const { ccclass, property } = _decorator;

/**
 * 角色管理中心
 */
@ccclass('EntityManager')
export class EntityManager extends Component {

  x:number = 0                  // 角色的当前位置坐标
  y:number = 0
  fsm:StateMachine        // 角色状态机
  // 数据与视图分离
  private _direction:DIRECTION_ENUM // 角色的朝向
  private _state:ENTITY_STATE_ENUM  // 角色当前的状态
  private type:ENTITY_TYPE_ENUM     // 角色的实体类型

  get direction() {
    return this._direction
  }

  /**
   * 设置角色朝向，先设置数据，再改变视图
   */
  set direction(newDirection:DIRECTION_ENUM) {
    // 先设置数据
    this._direction = newDirection
    // 然后设置状态机 number型 参数来改变视图
    this.fsm.setParams(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[this._direction])
  }

  get state() {
    return this._state
  }

  // 数据与视图分离，在设定状态时，都经过这里的分离
  set state(newState:ENTITY_STATE_ENUM) {
    // 设置数据
    this._state = newState
    // 执行视图切换
    this.fsm.setParams(this._state, true)
  }

  async init(params:IEntity) {
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    // 改变长款
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4)

    this.x = params.x
    this.y = params.y
    this.type = params.type
    this.direction = params.direction
    this.state = ENTITY_STATE_ENUM.IDLE

  }


  update() {
    this.node.setPosition(this.x * TILE_WIDTH - TILE_WIDTH * 1.5, -this.y * TILE_HEIGHT +  TILE_HEIGHT * 1.5)
  }


}
